<template>
    <Dialog v-model="dialogVisible" :title="dialogTitle">
        <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules"
                 label-width="95px">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="头像" prop="avatar">
                        <upload-img v-model="formData.avatar"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="名字" prop="name">
                        <el-input v-model="formData.name" placeholder="请输入名字"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="工号" prop="code">
                        <el-input v-model="formData.code" placeholder="请输入教师工号"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="性别" prop="sex">
                        <el-select v-model="formData.sex" placeholder="请选择">
                            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
                                       :label="dict.label" :value="dict.value"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="formData.mobile" placeholder="请输入手机号码"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="专业" prop="majorIds">
                        <el-select v-model="formData.majorIds" multiple placeholder="请选择专业" style="width: 100%" clearable>
                            <el-option v-for="item in majors" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="formData.status">
                            <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.YXT_ACTIVE_STATUS)" :key="dict.value"
                                      :label="dict.value">
                                {{ dict.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="预约权限" prop="canSub">
                        <el-radio-group v-model="formData.canSub">
                            <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value"
                                      :label="dict.value">
                                {{ dict.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
              <el-col :span="12">
                <el-form-item label="定位权限" prop="canLocation">
                  <el-radio-group v-model="formData.canLocation">
                    <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value"
                              :label="dict.value">
                      {{ dict.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="formData.remark" placeholder="请输入内容" type="textarea" :rows="3"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import {CommonStatusEnum} from '@/utils/constants'
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
import {useMemberApi} from "@/api/yxt/member";
import {useMajorApi} from "@/api/yxt/major";
import {useMessage} from "@/components/Notice";

defineOptions({name: 'YxtTeacherMemberForm'})

const {t} = useI18n()
const memberApi = useMemberApi();
const majorApi = useMajorApi();

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref({
    id: undefined,
    avatar: '',
    name: '',
    code: '',
    type: 1,
    majorIds: [],
    mobile: '',
    grade: '',
    canSub: CommonStatusEnum.ENABLE,
    canLocation: CommonStatusEnum.ENABLE,
    remark: '',
    sex: 1,
    status: CommonStatusEnum.ENABLE,
})
const formRules = reactive({
    name: [{required: true, message: '名字不能为空', trigger: ['blur', 'change']}],
    code: [{required: true, message: '教师工号不能为空', trigger: ['blur', 'change']}],
    type: [{required: true, message: '人员类型不能为空', trigger: ['blur', 'change']}],
    majorIds: [{required: true, message: '专业不能为空', trigger: ['blur', 'change']}],
    mobile: [
        {
            required: true,
            pattern: /^(?:(?:\+|00)86)?(?:1[3-9]\d{9}|193\d{8})$/,
            message: '请输入正确的手机号码',
            trigger: ['blur', 'change']
        }
    ]
})
const formRef = ref()
const majors = ref([])

const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    if (id) {
        formLoading.value = true
        try {
            formData.value = await memberApi.getMember(id)
        } finally {
            formLoading.value = false
        }
    }
    majors.value = await majorApi.getMajorList();
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = () => {
    formRef.value?.validate(async (valid: any) => {
        if (valid) {
            formLoading.value = true
            try {
                formData.value.type = 1
                const data = formData.value
                if (formType.value === 'create') {
                    await memberApi.createMember(data);
                    useMessage(t('common.createSuccess'));
                } else {
                    await memberApi.updateMember(data);
                    useMessage(t('common.updateSuccess'));
                }
                dialogVisible.value = false
                emit('success')
            } finally {
                formLoading.value = false
            }
        }
    });
}

const resetForm = () => {
    formData.value = {
        id: undefined,
        avatar: '',
        name: '',
        code: '',
        type: 1,
        majorIds: [],
        mobile: '',
        grade: '',
        remark: '',
        sex: 1,
        status: CommonStatusEnum.ENABLE,
        canSub: CommonStatusEnum.ENABLE,
        canLocation: CommonStatusEnum.ENABLE
    }
    formRef.value?.resetFields()
}
</script>
